package core;

import java.util.LinkedList;

import javax.swing.JOptionPane;

public class Agent extends AbstractAgent {
	LinkedList<String> actions;
	public Square.Type subgoal;
	
	public Agent(int x, int y, int opBound) {
		super(x, y, opBound);
		this.actions = new LinkedList<String>();
		this.goals = new LinkedList<Square.Type>();
		this.goals.add(Square.Type.meal);
		this.goals.add(Square.Type.smoking);
		this.goals.add(Square.Type.meal);
		this.goals.add(Square.Type.table);
		this.goals.add(Square.Type.flowers);
		this.subgoal = null;
	}

	public boolean goalReached() {
		return goalReached;
	}

	public void perceives(Perception perception) {
		this.perception = perception;
	}

	public void update() {
		behaviour();
	}
	
	public void behaviour() {
		if(this.goals.isEmpty() && subgoal == null && this.x == this.perception.floor.rooms.length - 1 && perception.floor.isDoor(this.x, this.y)) {
			this.goalReached = true;
			this.actions.clear();
			currAction = Action.Type.NOOP;
			return;
		}
		
		if (perception.floor.get(x, y) == subgoal) {
			currAction = Action.Type.DO;
			this.actions.clear();
			this.subgoal = null;
		} else if (this.actions == null || this.actions.isEmpty()) {
			if(!this.goals.isEmpty() && this.subgoal == null) {
				subgoal = this.goals.poll();
				JOptionPane.showMessageDialog(null, "I'm going to reach " + subgoal);
				DLV dlv = new DLV(this, this.perception.floor);
				dlv.writeDl();
				dlv.writeInitialPlan(10, subgoal.toString().toLowerCase(), this.maxSteps - 1);
				this.actions = dlv.getPlan(dlv.exec(subgoal.toString().toLowerCase(), this.maxSteps - 1));
			} else if(this.subgoal != null) {
				DLV dlv = new DLV(this, this.perception.floor);
				dlv.writeDl();
				dlv.writeInitialPlan(10, this.subgoal.toString().toLowerCase(), this.maxSteps - 1);
				this.actions = dlv.getPlan(dlv.exec(this.subgoal.toString().toLowerCase(), this.maxSteps - 1));
			} else {
				this.subgoal = null;
				JOptionPane.showMessageDialog(null, "I'm going to welcome Renata!");
				DLV dlv = new DLV(this, this.perception.floor);
				dlv.writeDl();
				dlv.writeInitialPlan(10, "ricevimento", -1);
				this.actions = dlv.getPlan(dlv.exec("ricevimento", -1));
			}
			currAction = Action.Type.NOOP;
		} else if(subgoal != null || !this.actions.isEmpty()) {
			String action = this.actions.poll();
			action = action.substring(5);
			int room = this.perception.floor.get(action.split(",")[0]);
			int cell = Integer.valueOf(action.split(",")[1].substring(0, 1));

			if (this.x > room)
				currAction = Action.Type.NORTH;
			else if (this.x < room)
				currAction = Action.Type.SOUTH;
			else if (this.y > cell)
				currAction = Action.Type.WEST;
			else if (this.y < cell)
				currAction = Action.Type.EAST;
			else
				currAction = Action.Type.NOOP;
		} else if(this.subgoal == null) {
			DLV dlv = new DLV(this, this.perception.floor);
			dlv.writeDl();
			dlv.writeInitialPlan(10, "ricevimento", -1);
			this.actions = dlv.getPlan(dlv.exec("ricevimento", -1));
			JOptionPane.showMessageDialog(null, "I'm going to welcome Renata!");
			currAction = Action.Type.NOOP;
		}
	}

	public void showActions() {
		for (int i = 0; i < actionList.size(); i++)
			System.out.println(actionList.get(i).type);
	}

	public Action.Type action() {
		actionList.add(new Action(currAction, x, y));
		return currAction;
	}
}